import 'package:flutter/material.dart';

class ProductItem extends StatefulWidget {
  String title;
  ProductItem({String title}) : this.title = title;

  @override
  _ProductItemState createState() => _ProductItemState(title: title);
}

class _ProductItemState extends State<ProductItem>
    with AutomaticKeepAliveClientMixin {
  String title;

  _ProductItemState({String title}) : this.title = title;

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;

  Widget _buildGrid(int i, int count, var retio) {
    return GridView.count(
      // 禁止滚动
//      physics: new NeverScrollableScrollPhysics(),
      shrinkWrap: true,
      childAspectRatio: retio,
      //横轴数量 这里的横轴就是x轴 因为方向是垂直的时候 主轴是垂直的
      crossAxisCount: i,
      padding: const EdgeInsets.all(0),
      //主轴间隔
      mainAxisSpacing: 2,
      //横轴间隔
      crossAxisSpacing: 2,
      children: _buildGridTileList(count),
    );
  }

  showSnackbar(String info) async {
    var snackBar = SnackBar(
      content: Text(info),
      backgroundColor: Colors.green,
    );
    Scaffold.of(context).showSnackBar(snackBar);
  }

  List<GestureDetector> _buildGridTileList(int count) {
    return List.generate(
      count,
      (int index) => GestureDetector(
          onTap: () {
            showSnackbar('haha$index');
          },
          child: Container(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Icon(
                  Icons.image,
                  size: 30,
                ),
                Text(
                  title,
                  style: TextStyle(fontSize: 14, color: Colors.black),
                )
              ],
            ),
          )),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: _buildGrid(4, 13, 1 / 1),
    );
  }
}
